/*
 * Copyright (C) 2012 monfeliscatus@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package felicatus.widgets;

import java.util.Date;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.text.format.DateFormat;
import android.util.Log;
import android.widget.RemoteViews;

public class TodayDateWidget extends AppWidgetProvider
{   
	private static final String TAG = "AppWidgetProvider";
	
	protected RemoteViews updateDateTimeText ( Context context )
	{
    	Date date = new Date();
    	CharSequence csDateTextY = DateFormat.format( "yyyy", date );
    	CharSequence csDateTextM = DateFormat.format( "MM/dd", date );
		
		//Log.i( TAG, "updateDateTimeText [" + csDateTextY + "-" + csDateTextM + "]");
		
		RemoteViews updateViewDate = new RemoteViews(context.getPackageName (), R.layout.widget);
		updateViewDate.setTextViewText(R.id.WidgetTextviewY, csDateTextY );
		updateViewDate.setTextViewText(R.id.WidgetTextviewM, csDateTextM );
		
		return updateViewDate;
	}	
	
	@Override
	public void onEnabled(Context context)
	{
		//Log.i( TAG, "onEnabled " );
		super.onEnabled(context);
		
		RemoteViews	updateViewDate;
		updateViewDate = updateDateTimeText( context );

		ComponentName thisWidget = new ComponentName( context, TodayDateWidget.class.getName() );
		AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
		int [] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
		appWidgetManager.updateAppWidget(appWidgetIds, updateViewDate);
	}
	
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 
    {
    	//Log.i( TAG, "OnUpdate called" );
    	super.onUpdate(context, appWidgetManager, appWidgetIds);

    	RemoteViews	updateViewDate;
		updateViewDate = updateDateTimeText( context );
		
		appWidgetManager.updateAppWidget(appWidgetIds, updateViewDate);
    } 

	@Override
    public void onReceive(Context context, Intent intent)
    {
		//Log.i( TAG, "onReceive called " );
		super.onReceive(context, intent);
    }
}
